/*
 * Created on Oct 26, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.inibb.app.sftp.impl.jsch;

import java.net.InetAddress;
import java.util.List;

import com.inibb.app.sftp.RemoteFileEntry;
import com.inibb.app.sftp.SFTPAuthInfo;
import com.inibb.app.sftp.exception.SFTPException;
import com.inibb.app.sftp.exception.SFTPIOException;
import com.inibb.app.sftp.exception.SFTPRemoteFileNotFoundException;
import com.inibb.app.sftp.exception.SFTPRemoteIOException;




/**
 * @author 838474
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JschSFTPConnectionExtLS extends JschSFTPConnection {
	
	OpenSSHDirListParser lsParser = null;
	/**
	 * 
	 */
	public JschSFTPConnectionExtLS() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * Make a SFTP connection to the target host and port with the given authentication info.
	 * @param addr - address of the target SFTP server
	 * @param port - port number of the target SFTP server
	 * @param auth - SFTPAuthInfo holding the credential for logging in the target SFTP server
	 * @throws SFTPIOException - When network related exception occurs
	 * @throws SFTPException - When SFTP related exception occurs
	 */
	public synchronized void connect(InetAddress addr, int port, SFTPAuthInfo auth)
		throws SFTPIOException, SFTPException 
	{
		super.connect(addr,port,auth);
		
		String sshCommand = System.getProperty("common.ssh.JschSFTPConnectionExtLS.sshCommand");
		lsParser = new OpenSSHDirListParser();
		if (sshCommand!=null)  lsParser.setSshCommand(sshCommand);
		lsParser.init(addr,port,auth);
	}

	/**
	 * Retrieve the directory listing of given path
	 * @param path - the <code>RemoteFileEntry</code> point to the directory / file you want to list, use <code>null</code> for current directort.
	 * @return The the <code>List</code> holding the list of <code>RemoteFileEntry</code>.
	 * @throws SFTPRemoteIOException - When I/O related exception occured in remote side
	 * @throws SFTPIOException - When network related exception occurs
	 * @throws SFTPException - When SFTP related exception occurs
	 */
	public List list(RemoteFileEntry path) 
		throws SFTPRemoteFileNotFoundException, SFTPRemoteIOException, SFTPIOException, SFTPException 
	{
		if (lsParser!=null) {
			List ls = lsParser.getDirList(path);
//System.err.println("~~~~~~~~~~~~~~~~~~~~ len="+ls.size());			
			return ls;
		}
		else return super.list(path);
	}

}
